package vn.edu.saigontech.ncms.web.bookstore;

import vn.edu.saigontech.ncms.model.bookstore.ImportReceipt;
import vn.edu.saigontech.ncms.service.ImportReceiptService;
import vn.edu.saigontech.ncms.service.UserService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateImportAction extends AbstractValidator {
	private Long id;
	private String number;
	private String date;
	private Long userId;

	@Override
	public String check() {
		if (Checker.isEmpty(this.number)) {
			this.sendKeyError("validator.required", "bookstore.import.id");
		}
		if(ImportReceiptService.INSTANCE.findById(this.id) == null) {
			ImportReceipt importReceipt = ImportReceiptService.INSTANCE.findByNumber(this.number);
			if(importReceipt != null) {
				this.sendKeyError("validator.exist", "bookstore.import.id");
			}
		}
		if(Checker.isEmpty(date)) {
			this.sendKeyError("validator.required", "date");
		}
		if(!Checker.isDate(this.date, this.i18n("format.date"))) {
			this.sendKeyError("validator.invalid", "date");
		}
		if(Checker.isNull(this.userId)) {
			this.sendKeyError("validator.required", "user");
		}
		if(Checker.isNull(UserService.INSTANCE.findById(this.userId))) {
			this.sendKeyError("validator.notExist", "user");
		}
		return this.toSuccess();
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
}
